package tushare

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"tushare/model"
	"tushare/model/tushare"
)

type Line struct{}

type Minute struct {
	Data MinuteData
}

type MinuteData struct {
	LastClose float64 `json:"last_close"`
	Items     []MinuteDataItems
	ItemsSize int64 `json:"items_size"`
}

type MinuteDataItems struct {
	Current   float64 `json:"current"`
	Timestamp int64   `json:"timestamp"`
	Percent   float64 `json:"percent"`
}

func (l Line) MinuteLine(tsCode string) Minute {
	// infoUrl := "https://xueqiu.com/S/SH600031"
	// client := &http.Client{}
	// var req *http.Request

	// req, err := http.NewRequest("GET", infoUrl, nil)
	// if err != nil {
	// 	fmt.Println(err)
	// }
	// // cookie1 := &http.Cookie{Name: "Cookie", Value: "device_id=c92012d02156b17983b58c25fc66f95e; s=cf12b5ssc3; __utmz=1.1626008984.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); Hm_lvt_1db88642e346389874251b5a1eded6e3=1626007578,1626488486; __utmc=1; remember=1; xq_a_token=19ca355c7aecac8d7d6a7077bcaa12ad7dfb12f8; xqat=19ca355c7aecac8d7d6a7077bcaa12ad7dfb12f8; xq_id_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJ1aWQiOjMyNzgyNzI0MjcsImlzcyI6InVjIiwiZXhwIjoxNjI5MTAxNTY5LCJjdG0iOjE2MjY1MDk1Njk0NjksImNpZCI6ImQ5ZDBuNEFadXAifQ.TNaMl-WcLt1i2DGGfH6301CRF_nVt43JFQM-onqHGdFI3ebRoSgftxnUhIZTtgqvXaHgmqsNyzxzJIjNzdAFy3oxfRCR5rIeKQctYJmcZ37SzcFRASKYPklpLZ9PPxNILCIBkI7wGSmqLS1b2rLs9W_ZDbKiA34XW6FtBwz9WMpX-TpaXiHJl7Bl7vlQXSo_mbNYRV83hLwzjzw8tjV4Q7OasjzxjPoKVcPOsRjcpUSbxkb0MoeP-Ul3lT4FP4goqXueoPBXCVb-Kk_4witHvyQOvUYjPrt0PoCkNvPDIxTH5HLmvBSqora7fBGHINV-QJU1cKYUcPzdO5Rgh34AZA; xq_r_token=9f5f22224e0f025f31ded18f6e302f13f01fae08; xq_is_login=1; u=3278272427; bid=0bc32ddaa0d3d0a4adc7836f5359abcb_kr7htaxw; __utma=1.1959968610.1626008984.1626488546.1626509590.3; snbim_minify=true; Hm_lpvt_1db88642e346389874251b5a1eded6e3=1626511680; acw_tc=2760820916265250386162891ef13eb5585c82c655e0b026986ec6c3b97461; is_overseas=0", HttpOnly: true}
	// // req.AddCookie(cookie1)
	// req.Header.Add("Cookie", "device_id=cc2bbf8bb25ea623664733b9c7456a35;")
	// res, err := client.Do(req)
	// if err != nil {
	// 	fmt.Println(err)
	// }
	// defer res.Body.Close()

	// if res.StatusCode != 200 {
	// 	fmt.Println("网络未正常响应httpCode:")
	// 	fmt.Println(res.StatusCode)
	// }
	url := fmt.Sprintf("https://stock.xueqiu.com/v5/stock/chart/minute.json?symbol=%s&period=1d", tsCode)
	return l.getJson(url)
}

func (l Line) getJson(url string) Minute {
	client := &http.Client{}
	var req *http.Request

	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		fmt.Println(err)
	}
	req.Header.Add("Cookie", "xq_a_token=fbc7bfef1e09beda4e5ad33d503c29e5aa980eb8;xq_r_token=692e7688d33c8aedebdcd521135ae3706921c61c")

	res, err := client.Do(req)
	if err != nil {
		fmt.Println(err)
	}
	defer res.Body.Close()

	if res.StatusCode != 200 {
		fmt.Println("网络未正常响应httpCode:")
		fmt.Println(res.StatusCode)
	}

	b, _ := ioutil.ReadAll(res.Body)

	str := []byte(b)
	jsonData := Minute{}
	err = json.Unmarshal(str, &jsonData)
	if err != nil {
		fmt.Println(err)
	}

	return jsonData
}

func MinuteAnalyse() {
	stocks := model.StockList()
	for _, v := range stocks {
		m := tushare.MinuteInfo(v.TsCode, "2022-01-14")

		str := []byte(m.MinuteJson)
		jsonData := Minute{}
		err := json.Unmarshal(str, &jsonData)
		if err != nil {
			fmt.Println(err)
		}
		min := 0.0
		max := 0.0
		for _, item := range jsonData.Data.Items {
			if item.Percent < min {
				min = item.Percent
			}
			if item.Percent > max {
				max = item.Percent
			}
		}
		if min < -5 && max > 5 {
			fmt.Println(v.TsCode)
		}
	}
}
